#include "../common/httplib.h"
#include "compile_run.hpp"
using namespace std;

void Usage(string proc)
{
    cerr << "Usage: " << "\n\t" << proc << " port" << endl;
}

//./compile_server port
int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }
    httplib::Server svr;

    svr.Post("/compile_and_run", [](const httplib::Request &req, httplib::Response &resp){
        // 用户请求的服务正文是我们想要的json string
        string in_json = req.body;
        string out_json;
        if(!in_json.empty())
        {
            CompileAndRun::Start(in_json, &out_json);
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });
    svr.listen("0.0.0.0", atoi(argv[1]));
    return 0;
}